package com.limingru.newbee.api;

import com.limingru.newbee.api.param.CartModifyCountParam;
import com.limingru.newbee.api.param.CartSaveGoodsParam;
import com.limingru.newbee.api.param.CartToSettleParam;
import com.limingru.newbee.api.vo.CartItemVO;
import com.limingru.newbee.common.NewBeeMallException;
import com.limingru.newbee.common.ServiceResultEnum;
import com.limingru.newbee.config.annotation.TokenToUser;
import com.limingru.newbee.entity.CartItem;
import com.limingru.newbee.entity.User;
import com.limingru.newbee.service.CartService;
import com.limingru.newbee.util.Result;
import com.limingru.newbee.util.ResultGenerator;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/v1")
public class CartAPI {

    @Resource
    CartService cartService;

    /**
     * 向购物车添加商品
     * @param cartSaveGoodsParam
     * @param user
     * @return
     */
    @PostMapping("/cart/add")
    @ApiOperation("向购物车中添加商品")
    public Result saveNewGoodsToCart(@RequestBody CartSaveGoodsParam cartSaveGoodsParam, @TokenToUser User user) {
        String saveResult = cartService.saveGoodsInCart(cartSaveGoodsParam, user.getUserId());
        if (saveResult.equals(ServiceResultEnum.SUCCESS.getText())) {
            return ResultGenerator.generateSuccessResult(saveResult);
        }
        return ResultGenerator.generateFailResult(saveResult);
    }

    /**
     * 获取购物车列表
     * @param user
     * @return
     */
    @GetMapping("/cart/list")
    @ApiOperation(value = "获取购物车列表")
    public Result<List<CartItemVO>> cartList(@TokenToUser User user) {
        List<CartItemVO> cartList = cartService.getCartList(user.getUserId());
        return ResultGenerator.generateSuccessResult(cartList);
    }

    /**
     * 修改单件商品数量
     * @return
     */
    @PostMapping("/cart/modifyCount")
    @ApiOperation(value = "修改单项数量", notes = "传参为单项id和数量")
    public Result modifyCount(@RequestBody CartModifyCountParam cartModifyCountParam, @TokenToUser User user) {
        String modifyResult = cartService.modifyCount(cartModifyCountParam, user.getUserId());
        if (ServiceResultEnum.SUCCESS.getText().equals(modifyResult)) {
            return ResultGenerator.generateSuccessResult(modifyResult);
        }
        return ResultGenerator.generateFailResult(modifyResult);
    }

    @GetMapping("/cart/remove/{cartItemId}")
    @ApiOperation(value = "购物车删除购物项")
    public Result removeCartItem(@PathVariable Long cartItemId, @TokenToUser User user) {
        CartItem cartItem = cartService.findCartItemById(cartItemId);
        if (!cartItem.getUserId().equals(user.getUserId())) {
            NewBeeMallException.fail(ServiceResultEnum.FORBID.getText());
        }
        String result = cartService.removeCartItem(cartItem);
        if (ServiceResultEnum.SUCCESS.getText().equals(result)) {
            return ResultGenerator.generateSuccessResult(result);
        }
        return ResultGenerator.generateFailResult(result);
    }

    @PostMapping("/cart/settle")
    @ApiOperation(value = "根据购物项id数组查询购物项明细", notes = "确认订单页使用")
    public Result toSettle(@RequestBody CartToSettleParam cartToSettleParam, @TokenToUser User user) {
        if (cartToSettleParam.getCartItemIds().length < 1) {
            NewBeeMallException.fail("参数异常");
        }
        List<CartItemVO> result = cartService.getCartItemsForSettle(Arrays.asList(cartToSettleParam.getCartItemIds()), user.getUserId());
        int total = 0;
        for (CartItemVO c : result) {
            total += c.getGoodsCount() * c.getSellingPrice();
        }
        if (total < 0) {
            NewBeeMallException.fail("价格异常");
        }
        return ResultGenerator.generateSuccessResult(result);
    }
}
